package org.huangxq.bigscreen.controller.config

import com.alibaba.fastjson2.JSONArray
import jakarta.annotation.Resource
import org.huangxq.bigscreen.data.ComplexIndConfigBO
import org.huangxq.bigscreen.data.ComplexIndConfigData
import org.huangxq.bigscreen.data.SimpleIndConfigData
import org.huangxq.bigscreen.data.SqlConfig
import org.huangxq.bigscreen.service.ComplexIndConfigService
import org.huangxq.bigscreen.service.RePortConfigService
import org.huangxq.bigscreen.service.SimpleIndConfigService
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
@Description: 数据源配置请求
@author: 黄显清
@Date: 2022-07-21
@version： 1.0
 */

@RestController
@RequestMapping("/config/report")
class RePortController {

    @Resource
    private lateinit var rePortConfigService: RePortConfigService
    @Resource
    private lateinit var simpleIndConfigService: SimpleIndConfigService
    @Resource
    private lateinit var complexIndConfigService: ComplexIndConfigService

    private val log = LoggerFactory.getLogger("log")

    @RequestMapping("/getReportTitle")
    fun getReportTitle(code: String) = try {
        val name = rePortConfigService.getReportTitle(code)
        mapOf(
            "status" to 200,
            "data" to name
        )
    }catch (e: Exception){
        log.error(e.message)
        mapOf(
            "status" to -1,
            "data" to e.message
        )
    }

    @RequestMapping("/saveReportTitle")
    fun saveReportTitle(code: String, name: String) = try {
        rePortConfigService.saveReportTitle(code, name)
        "success"
    }catch (e: Exception){
        log.error(e.message)
        e.message
    }

    @RequestMapping("/getSimpleIndConfig")
    fun getSimpleIndConfig(reportId: String, indId: String) = try {
        mapOf(
            "status" to 200,
            "data" to simpleIndConfigService.getIndConfig(reportId, indId)
        )
    }catch (e: Exception){
        log.error(e.message)
        mapOf(
            "status" to -1,
            "data" to e.message
        )
    }

    @RequestMapping("/saveSimpleIndConfig")
    fun saveSimpleIndConfig(@RequestBody simpleIndConfigData: SimpleIndConfigData) = try {
        simpleIndConfigService.saveIndConfig(simpleIndConfigData)
        "success"
    }catch (e: Exception){
        log.error(e.message)
        e.message
    }

    @RequestMapping("/getComplexIndConfig")
    fun getComplexIndConfig(reportId: String, indId: String) = try {
        val complexIndConfigData = complexIndConfigService.getIndConfig(reportId, indId)
        mapOf(
            "status" to 200,
            "data" to ComplexIndConfigBO(
                complexIndConfigData.key,
                complexIndConfigData.title,
                complexIndConfigData.dbID,
                JSONArray.parseArray(complexIndConfigData.sqlConfig, SqlConfig::class.java),
                complexIndConfigData.real
            )
        )
    }catch (e: Exception){
        log.error(e.message)
        mapOf(
            "status" to -1,
            "data" to e.message
        )
    }

    @RequestMapping("/saveComplexIndConfig")
    fun saveComplexIndConfig(@RequestBody complexIndConfigBO: ComplexIndConfigBO) = try {
        complexIndConfigService.saveIndConfig(ComplexIndConfigData(
            complexIndConfigBO.key,
            complexIndConfigBO.title,
            complexIndConfigBO.dbID,
            JSONArray.toJSONString(complexIndConfigBO.sqlConfig),
            complexIndConfigBO.real
        ))
        "success"
    }catch (e: Exception){
        log.error(e.message)
        e.message
    }
}